/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.afraid.poison.common;

import java.io.File;
import java.util.Arrays;
import java.util.EventListener;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Andreas Schnaiter <rc.poison@gmail.com>
 */
public class FileSystemWalker {

	public static interface Predicate {

		public boolean accept(File file);
	}

	public static class DefaultPredicate implements Predicate {

		public boolean accept(File file) {
			return true;
		}
	}

	public static interface Action {

		public void perform(File file);
	}
	private final File path;

	public FileSystemWalker(File path) {
		this.path=path;
	}

	public void walk(Action action, Predicate predicate) {
		if (path.isDirectory()) {
			walk(path, action, predicate);
		} else {
			if (predicate.accept(path)) {
				action.perform(path);
			}
		}
	}

	private void walk(File path, Action action, Predicate predicate) {
		for (File f : path.listFiles()) {
			if (predicate.accept(f)) {
				action.perform(f);
				if (f.isDirectory()) {
					walk(f, action, predicate);
				}
			}
		}
	}
}
